package com.adinnc.Util;

import java.util.ArrayList;

import com.adinnc.vo.PassengerDetails;

import android.annotation.SuppressLint;

/**
 * @author adi
 *
 */
@SuppressLint("DefaultLocale")
public class StringUtils {
	
	public enum CHARTSTATUS {
		can,mod,
		cnf,confirmed,
		rac,
		waitinglist,
		rlwl,
		gnwl,
		pqwl,
		regret,
		released,
		r;
	}

	public static String trim(String string) {
		if (string == null) {
			return "";
		}
		return string.trim();
	}

	public static String replace(String originalString, String partToMatch,
			String replacement) {
		StringBuffer buffer = new StringBuffer(originalString.length());
		buffer.append(originalString);

		int indexOf = buffer.indexOf(partToMatch);
		while (indexOf != -1) {
			buffer = buffer.replace(indexOf, indexOf + partToMatch.length(),
					replacement);
			indexOf = buffer.indexOf(partToMatch);
		}

		return buffer.toString();
	}

	/**
	 * Trim leading and trailing whitespace from the given String.
	 * 
	 * @param str
	 *            the String to check
	 * @return the trimmed String
	 * @see java.lang.Character#isWhitespace
	 */
	public static String trimWhitespace(String str) {
		if (str.length() == 0) {
			return str;
		}
		StringBuilder sb = new StringBuilder(str);
		while (sb.length() > 0 && Character.isWhitespace(sb.charAt(0))) {
			sb.deleteCharAt(0);
		}
		while (sb.length() > 0
				&& Character.isWhitespace(sb.charAt(sb.length() - 1))) {
			sb.deleteCharAt(sb.length() - 1);
		}
		return sb.toString();
	}

	/**
	 * Test if the given String starts with the specified prefix, ignoring
	 * upper/lower case.
	 * 
	 * @param str
	 *            the String to check
	 * @param prefix
	 *            the prefix to look for
	 * @see java.lang.String#startsWith
	 */
	public static boolean startsWithIgnoreCase(String str, String prefix) {
		if (str == null || prefix == null) {
			return false;
		}
		if (str.startsWith(prefix)) {
			return true;
		}
		if (str.length() < prefix.length()) {
			return false;
		}
		String lcStr = str.substring(0, prefix.length()).toLowerCase();
		String lcPrefix = prefix.toLowerCase();
		return lcStr.equals(lcPrefix);
	}

	/**
	 * Test whether the given string matches the given substring at the given
	 * index.
	 * 
	 * @param str
	 *            the original string (or StringBuilder)
	 * @param index
	 *            the index in the original string to start matching against
	 * @param substring
	 *            the substring to match at the given index
	 */
	public static boolean substringMatch(CharSequence str, int index,
			CharSequence substring) {
		for (int j = 0; j < substring.length(); j++) {
			int i = index + j;
			if (i >= str.length() || str.charAt(i) != substring.charAt(j)) {
				return false;
			}
		}
		return true;
	}
	
	public static String concatObject(ArrayList<PassengerDetails> obj){
		String s = new String("");
		for(PassengerDetails pD : obj){
			String onePas = pD.sno + "- " + pD.bookingStatus + "- " + pD.currentStatus + " ;" ;
			s = s + onePas;
		}
		return s;
	}
	
	public static String toCamelCase( String str ) {
        StringBuffer buffer = new StringBuffer(str);
        int i = 0;
        while( i < buffer.length() ) {
            if( buffer.charAt(i) == '_' || Character.isWhitespace( buffer.charAt(i) ) || buffer.charAt(i) == '.' || buffer.charAt(i) == '-' ) {
                buffer.deleteCharAt( i );
                if( Character.isLetter( buffer.charAt(i) ) ) {
                    buffer.setCharAt( i, Character.toUpperCase(buffer.charAt(i)) );
                    i++;
                }
            } else {
                i++;
            }
        }
        return buffer.toString();
    }
	
	public static CHARTSTATUS chartStatus(String str){
		if(str == null || str.trim().length() == 0)
			return null;
		str = str.trim();
		str = str.split(" ")[0];
		if (startsWithIgnoreCase(str, "can") || startsWithIgnoreCase(str, "mod")) {
			return CHARTSTATUS.can;
		} else if (startsWithIgnoreCase(str, "cnf") || startsWithIgnoreCase(str, "confirmed")) {
			return CHARTSTATUS.cnf;
		} else if (startsWithIgnoreCase(str, "rac")) {
			return CHARTSTATUS.rac;
		} else if (startsWithIgnoreCase(str, "wl")) {
			return CHARTSTATUS.waitinglist;
		} else if (startsWithIgnoreCase(str, "rnwl")) {
			return CHARTSTATUS.rlwl;
		} else if (startsWithIgnoreCase(str, "gnwl")) {
			return CHARTSTATUS.gnwl;
		} else if (startsWithIgnoreCase(str, "pqwl")) {
			return CHARTSTATUS.pqwl;
		} else if (startsWithIgnoreCase(str, "regret") || startsWithIgnoreCase(str, "wl")) {
			return CHARTSTATUS.regret;
		} else if (startsWithIgnoreCase(str, "released")) {
			return CHARTSTATUS.released;
		} else if (startsWithIgnoreCase(str, "r")) {
			return CHARTSTATUS.r;
		} else {
			//if none of the them are excuted means, chart is prepared
			return CHARTSTATUS.cnf;
		}
	}
	
}
